package com.krain.ddbb.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.krain.corelibrary.widget.loadingView.LoadingView;
import com.krain.corelibrary.widget.recyleview.RefreshRecyclerView;
import com.krain.corelibrary.widget.recyleview.implement.OnRecyclerItemListener;
import com.krain.ddbb.R;
import com.krain.ddbb.adapter.UserAddresAdapter;
import com.krain.ddbb.adapter.UserWorkAdapter;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.BaseEntity;
import com.krain.ddbb.entity.PersonArdessBean;
import com.krain.ddbb.entity.UserInfoBean;
import com.krain.ddbb.entity.WorkNameBean;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ex-kuangxiaomin101 on 2016-01-13.
 */
@EActivity(R.layout.activity_user_address)
public class UserAddresActivity extends BaseActivity {

    @ViewById(R.id.loadView)
    LoadingView mLoadingView;
    @ViewById(R.id.act_user_addres_recyleview)
    RefreshRecyclerView mRefreshRecyclerView;
    @ViewById(R.id.act_addres_btn_add)
    Button mAddAddres;
    UserAddresAdapter adapter;


     private List<PersonArdessBean> mEntityList;

    @AfterViews
    void afterView() {
        mRefreshRecyclerView.showRefreshing(false);
        mRefreshRecyclerView.setIsLoadMore(false);
        mRefreshRecyclerView.getRecyclerView().setHasFixedSize(true);
        getAddressType(mEntityList,mRefreshRecyclerView,mLoadingView);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            getAddressType(mEntityList,mRefreshRecyclerView,mLoadingView);
        }
    }

    /**
     * 添加用户地址
     */
    @Click(R.id.act_addres_btn_add)
    void onClickAddAddres() {
        //postAddres();
        AddAddresActivity.jumpTothisActivity(this);
    }

    @Background
    void postAddres(){
        BaseEntity entity = AppApi.getsInstance(mContext).postAddress(app.getmUserinfo().getAccess_token(),"qweeee");
        if(entity!=null){
            if((entity.getCode().toString()).equals(entity.isOk())){
                baseUtil.showSnackBar(this, "用户地址添加成功！");
            }else{
                baseUtil.showSnackBar(this, entity.getMsg());
            }
        }
    }


    @Background
    protected void getAddressType( List<PersonArdessBean> mEntityList,RefreshRecyclerView mRefreshRecyclerView,LoadingView loadingView) {
        List<PersonArdessBean> bean = AppApi.getsInstance(mContext).getAddressList(app.getmUserinfo().getAccess_token());
        if(bean!=null){
            setAdapter(bean,mRefreshRecyclerView,loadingView);
        }
    }

    @UiThread
    protected void setAdapter( List<PersonArdessBean> mEntityList,RefreshRecyclerView mRefreshRecyclerView,LoadingView loadingView) {
         adapter = new UserAddresAdapter(mEntityList, mContext);
        mRefreshRecyclerView.setAdapter(adapter);
        adapter.setOnItemListener(new MyRecyclerItemListener(mEntityList));
        loadingView.showContentView(mRefreshRecyclerView);
    }

    /**
     * 列表的点击事件
     */
    class MyRecyclerItemListener extends OnRecyclerItemListener {
        List<PersonArdessBean> mEntityList;

        public MyRecyclerItemListener( List<PersonArdessBean> mEntityList) {
            this.mEntityList = mEntityList;
        }

        @Override
        public void onItemClick(final int position, int viewType, RecyclerView.ViewHolder holder, View v) {

        }
        @Override
        public void onItemLongClick(final int position, int viewType, RecyclerView.ViewHolder holder, View v) {
            super.onItemLongClick(position, viewType, holder, v);
            showDialog(mContext, "警告", "此操作将删除当前的地址", "确认", "取消", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteAddress(String.valueOf(((PersonArdessBean)adapter.getItem(position)).getId()),position);
                }
            },null,true);

        }
    }

    public static void jumpTothisActivity(BaseActivity activity) {
        Intent intent = new Intent(activity, UserAddresActivity_.class);
        activity.startActivity(intent);

    }



    @Background
    void deleteAddress(String id,int pos){
        showProgressDialog();
        BaseEntity entity =  AppApi.getsInstance(mContext).deleteUserAddress(app.getmUserinfo().getAccess_token(),id);
        if (entity != null) {
            baseUtil.showSnackBar(mContext,"删除地址成功");
            List<PersonArdessBean> addressList = new ArrayList<>(adapter.getList());
            addressList.remove(pos);
            setAdapter(addressList,mRefreshRecyclerView,mLoadingView);

        }
        dismissProgressDialog();
    }


    @Override
    protected String getToolbarTitle() {
        return "我的地址";
    }
}
